import java.util.Scanner
val scanner = Scanner(System.`in`)
class Station(var x:Int, var y:Int){}
class Chess(var station:Station,var type:String)
class Player(var name:String,var chess:Chess){
    fun luozi(){
        print("请输入行")
        var x=scanner.nextInt()
        print("请输入列")
        var y=scanner.nextInt()
        chess.station.x=x
        chess.station.y=y
    }
}
class Ruler(var board:Broad,var point:Int,var player1:Player,var player2:Player){
    fun win() {
        var num=0
        var array=board.array
        var size=board.size
        for (i in 0 until size) {
            for (j in 0 until size) {
                if(array[i][j]=="+"){
                    num++
                }
                if (array[i][j] == "⊙" || array[i][j] == "●") {
                    for (x in -1..1) {
                        for (y in -1..1) {
                            if (x != 0 || y != 0) {
                                if (i + point * x in 0..size-1 && j + (point) * y in 0..size-1) {
                                    var end=1;
                                    for (z in 1 .. point-1){
                                        if(array[i][j]==array[i + z * x][j +z * y ]){
                                            end++
                                        }
                                    }
                                    if(end==point){
                                        if(array[i][j]=="⊙"){
                                            println("黑棋胜利")
                                        }else if(array[i][j]=="●")
                                        {
                                            println("白棋胜利")
                                        }
                                        System.exit(0)
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        if(num==0){
            println("和棋！游戏结束！")
            System.exit(0)
        }
    }
    fun accept(player:Player): Boolean {
        if(board.array[player.chess.station.x-1][player.chess.station.y-1]=="⊙"||board.array[player.chess.station.x-1][player.chess.station.y-1]=="●")
        {
            println("不可以覆盖下棋，请重新输入")
        }else if(player.chess.station.x-1 in 0..board.size&&player.chess.station.y-1 in 0..board.size){
            board.array[player.chess.station.x-1][player.chess.station.y-1]=player.chess.type
            return false
        }else{
            println("输入位置越界，请重新输入")
        }
        return true
    }
}
class Broad(var size:Int){
    var array = Array(size) { Array(size) { "+" } }
    fun  printBoard(){
        for (row in array){
            for(cell in row){
                print("$cell  ")
            }
            println()
        }
    }
}
class Player_manerger{
    fun game(){
        println("请输入棋盘的大小")
        var size=scanner.nextInt()
        println("请输入你要玩几子棋")
        var point=scanner.nextInt()
        var board=Broad(size)
        var station1=Station(0,0)
        var station2=Station(1,1)
        var chess1=Chess(station1,"⊙")
        var chess2=Chess(station2,"●")
        var player1=Player("张三",chess1)
        var player2=Player("李四",chess2)
        var ruler=Ruler(board,point,player1,player2)
        while(true){
            board.printBoard()
            if(count%2==1){
                player1.luozi()
                if(ruler.accept(player1)){
                    count++
                }
            }else if(count%2==0){
                player2.luozi()
                if(ruler.accept(player2)){
                    count++
                }
            }
            ruler.win()
            count++
        }
    }
}
var count=1
fun main(){
    var game=Player_manerger()
    game.game()
}